﻿using AccessCompare.Entities;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AccessCompare
{
    public class TreeViewBuilder
    {
        #region VARIABLE DECLARATIONS

        private int intLevel = 0;
        private SchemaComparer _comparer;

        #endregion

        #region CONSTRUCTOR

        /// <summary>
        /// Builds the TreeView of Source database and Destination database. Source db path and Destination db path needs to be provided in order
        /// to be filled
        /// </summary>
        /// <param name="source">The source database path</param>
        /// <param name="destination">The destination database path</param>
        public TreeViewBuilder(string source, string destination)
        {
            if (string.IsNullOrEmpty(source) && string.IsNullOrEmpty(destination))
            {
                throw new Exception("Source and Destination Database path needs to be provided!");
            }
            else
            {
                _comparer = new SchemaComparer(source, destination);
            }
        }

        #endregion

        #region PUBLIC METHODS

        /// <summary>
        /// Fills the provided tree views with data
        /// </summary>
        /// <param name="source">TreeView that needs to display source database schema</param>
        /// <param name="destination">TreeView that needs to display destination database schema</param>
        public void FillTreeViews(TreeView source, TreeView destination)
        {
            _comparer.Compare();

            BuildTreeNodes(_comparer.SourceSchema, source);

            BuildTreeNodes(_comparer.DestinationSchema, destination);
        }


        #endregion

        #region PRIVATE METHODS

        /// <summary>
        /// Builds the Tables nodes
        /// </summary>
        /// <param name="data">Tables list data</param>
        /// <param name="tw">TreeView that needs to be filled</param>
        private void BuildTreeNodes(List<Table> data, TreeView tw)
        {
            foreach (Table d in data)
            {
                TreeNode t = BuildNode(d.Name);
                t.Tag = 0;

                if (d.TableStatus != Status.Equal)
                {
                    t.ForeColor = Color.Red;
                }

                FillChildren(t, d);
                tw.Nodes.Add(t);
            }
        }

        /// <summary>
        /// Fills the childrens of each node
        /// </summary>
        /// <param name="parent">Parent node that needs to be filled</param>
        /// <param name="table">Table data that contains the columns</param>
        private void FillChildren(TreeNode parent, Table table)
        {
            intLevel++;

            if (table.Columns.Count == 0)
            {
                parent.ImageIndex = 2;
                parent.SelectedImageIndex = 2;
            }

            foreach (Column column in table.Columns)
            {
                TreeNode t = BuildNode(String.Format("Column Name: {0}, Type: {1}", column.Name, column.ColumnType));
                t.Tag = intLevel;

                if (column.ColumnStatus != Status.Equal)
                {
                    t.ForeColor = Color.Red;
                }

                parent.Nodes.Add(t);
            }

            intLevel--;
        }

        /// <summary>
        /// Creates a new TreeNode
        /// </summary>
        /// <param name="text">The display text of the node</param>
        /// <returns>TreeNode with display text</returns>
        private TreeNode BuildNode(string text)
        {
            return new TreeNode(text);
        }

        #endregion
    }
}
